from pyflink.datastream import StreamExecutionEnvironment, FlatMapFunction

env = StreamExecutionEnvironment.get_execution_environment()

env.set_parallelism(1)

lines_ds = env.read_text_file('../../data/word.txt')

#1、lambda表达式

words_ds = lines_ds.flat_map(lambda line: line.split(','))

# words_ds.print()

#2、函数
def flat_map_fun(line):
    for word in line.split(','):
        yield word


lines_ds.flat_map(flat_map_fun) #.print()

# 3、使用类的方式
# 多了open和close方法，可以用于初始化操作，比如创建数据库连接

class FlatMapClass(FlatMapFunction):

    def flat_map(self, line):
        for word in line.split(','):
            yield word

lines_ds.flat_map(FlatMapClass()).print()


env.execute()